﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using WeChatApp.Models;
using WeChatApp.Helper;
using System.Transactions;
using static WeChatApp.Repositories.DictRepository;

namespace WeChatApp.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class PostTestController : BaseController
    {
        [HttpGet]
        public JsonResult Get()
        {
            int sta = ConstantEnum.END.GetHashCode();
            List<GradeModels> Grade = new List<GradeModels>();
            try
            {
                Grade = RepositoryFactoryInst.DBModel.Queryable<GradeModels>().Where(c => c.PID != null).ToList();
                return JsonResultHelper(Grade, Grade.Count);
            }
            catch (Exception ex)
            {
                return JsonResultHelper(Grade, Grade.Count, "查询失败," + ex.Message);
            }
        }
        //[HttpPost]
        //public JsonResult GetByid(int id)
        //{
        //    var list = new List<Some>{
        //        new Some{ id=1,value="12"},
        //        new Some{id=2,value="44"}
        //    };
        //    if(id>0)
        //        list = list.Where(c=>c.id==id).ToList();

        //    return JsonResultHelper(true, list);
        //}

        [HttpPost]
        public string Post()
        {
            try
            {

                List<GradeModels> grade = JsonConvert.DeserializeObject<List<GradeModels>>(Request.Form["data"]);
                var ret = RepositoryFactoryInst.DBModel.Insertable<GradeModels>(grade).ExecuteCommand();
                if (ret > 0)
                    return "新增成功";
                else
                    return "新增失败";
            }
            catch (Exception ex)
            {
                return "新增失败," + ex.Message;
            }
        }
        [HttpPut]
        public JsonResult PutByID(string id)
        {
            return JsonResultHelper(true, "");
        }
        [HttpDelete]
        public JsonResult Delete(string ids)
        {
            try
            {
                //加长事务时间
                TransactionOptions options = new TransactionOptions();
                options.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
                options.Timeout = new TimeSpan(0, 20, 0);//20分钟
                using (TransactionScope trans = new TransactionScope(TransactionScopeOption.Required, options))
                {
                    List<string> idList = JsonConvert.DeserializeObject<List<string>>(ids);
                    foreach (string id in idList)
                    {
                        RepositoryFactoryInst.DBModel.Deleteable<GradeModels>().Where(c => c.ID == id).ExecuteCommand();
                    }
                    trans.Complete();
                    return JsonResultHelper(true, "删除成功");
                }
            }
            catch (Exception ex)
            {
                return JsonResultHelper(false, "删除失败," + ex.Message);
            }
        }
    }
    public class Some
    {
        public int id { get; set; }
        public string value { get; set; }
    }
}